package com.xiaomaoguai.guava.precondition;

import org.junit.Test;

import com.google.common.base.Preconditions;

/**
 * @Description :Preconditions优雅的检验参数
 * @FileName: PreconditionTest.java
 * @Author :WeiHui.Zhang
 * @Data : 2015年9月20日 下午3:46:35
 * @Version:V1.00
 */
public class PreconditionTest {

	@Test
	public void Preconditions() throws Exception {

		getPerson(8, "peida");

		getPerson(-9, "peida");

		getPerson(8, "");

		getPerson(8, null);
	}

	public static void getPerson(int age, String name) throws Exception {
		if (age > 0 && name != null && name.isEmpty() != true) {
			System.out.println("a person age:" + age + ",neme:" + name);
		} else {
			System.out.println("参数输入有误！");
		}
	}

	@Test
	public void Preconditions2() throws Exception {

		getPersonByPrecondition(8, "peida");

		try {
			getPersonByPrecondition(-9, "peida");
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}

		try {
			getPersonByPrecondition(8, "");
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}

		try {
			getPersonByPrecondition(8, null);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}

	public static void getPersonByPrecondition(int age, String name) throws Exception {
		Preconditions.checkNotNull(name, "name");
		Preconditions.checkArgument(name.length() > 0, "name为\'\'");
		Preconditions.checkArgument(age > 0, "age 必须大于0");
		System.out.println("a person age:" + age + ",name:" + name);
	}

}
